package main

import (
	"account-manage/base"
	"account-manage/sql"
	"fmt"
	"log"
	"os"
	"os/exec"
	"strconv"

	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/app"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/dialog"
	"fyne.io/fyne/v2/layout"
	"fyne.io/fyne/v2/widget"
	"github.com/yanyiwu/gojieba"
)

type config struct {
	ChildWindow         fyne.Window
	List                []sql.List
	IndexText           *widget.Label
	IdText              *widget.Label
	TypeText            *widget.Label
	NameText            *widget.Label
	UsernameText        *widget.Button
	PasswordText        *widget.Label
	AppidText           *widget.Label
	AppSecretText       *widget.Label
	UrlText             *widget.Label
	DescText            *widget.Label
	VScroll             *container.Scroll
	welcomeLayout       *fyne.Container
	infoLayout          *fyne.Container
	ParticipleLayout    *fyne.Container
	WordsList           []string
	GridRows            int
	WordListLayout      *widget.List
	ListLayout          *widget.List
	UsernameCheckStatus bool
}

var cfg config

/* // 抖音服务商工具路径,指定要打开的.exe文件的路径
var tiktokServiceProviderToolPath string = "D:/project/go/src/tiktok-audit-tool/tiktok-audit-tool.exe"

// 设置工作目录
var tiktokServiceWorkDirectory string = "D:/project/go/src/tiktok-audit-tool/" */

// 抖音服务商工具路径,指定要打开的.exe文件的路径,// 设置工作目录
var tatPath = [2]string{"D:/raymond/tiktok-audit-tool/", "tiktok-audit-tool.exe"}

func main() {

	base.Inits()
	sql.ConnectDB()
	a := app.New()
	w := a.NewWindow("账号管理 1.1.5")

	// cfg.login(w)

	cfg.createMenuItems(w)

	ListLayout := cfg.makeButtonList()

	allLayout := cfg.makeUI(w, ListLayout)
	// win.Refresh()

	w.SetContent(allLayout)

	// 设置窗口大小,
	w.Resize(fyne.NewSize(870, 530))
	// 窗口居中
	w.CenterOnScreen()
	//设置主窗口
	w.SetMaster()
	//等价于w.Show()a.Run()
	w.ShowAndRun()
	// w.Show()
	// a.Run()
}

// 创建主菜单
func (app *config) createMenuItems(win fyne.Window) {
	newMenuItem := fyne.NewMenuItem("添加...", app.openAddWindow(win, "add"))
	exportMenuItem := fyne.NewMenuItem("导出...", app.exportFile(win))
	fileMenu := fyne.NewMenu("文件", newMenuItem, exportMenuItem)

	menu := fyne.NewMainMenu(fileMenu)

	win.SetMainMenu(menu)
}

// 打开添加窗口
func (app *config) openAddWindow(win fyne.Window, types string) func() {
	return func() {
		//窗口存在就显示
		if app.ChildWindow != nil {
			// app.ChildWindow.Close()
			app.ChildWindow.Show()
			return
		}
		w := fyne.CurrentApp().NewWindow(types)

		app.ChildWindow = w
		var typeText string
		typeName := widget.NewLabel("type:     ")
		typeSelect := widget.NewSelect([]string{"微信", "抖音", "快手", "支付宝", "其他"}, func(s string) {
			typeText = s
		})
		typeSelect.PlaceHolder = "请选择类型"
		typeRow := container.NewBorder(nil, nil, typeName, layout.NewSpacer(), typeSelect)

		var typeStatusText string
		typeStatusName := widget.NewLabel("状态:     ")
		typeStatusSelect := widget.NewSelect([]string{"正常", "废弃", "其他"}, func(s string) {
			typeStatusText = s
		})
		typeStatusSelect.PlaceHolder = "请选择状态"
		typeStatusRow := container.NewBorder(nil, nil, typeStatusName, layout.NewSpacer(), typeStatusSelect)
		fmt.Println(typeStatusText)

		name := widget.NewLabel("name:     ")
		nameInput := widget.NewEntry()
		//多行输入
		nameInput.MultiLine = true
		nameInput.Wrapping = 2 // 开启自动换行
		nameRow := container.NewBorder(nil, nil, name, layout.NewSpacer(), nameInput)

		// nameRow := container.NewBorder(nil, nil, nil, name, nil, nameInput)

		username := widget.NewLabel("username: ")
		usernameInput := widget.NewEntry()
		usernameRow := container.NewBorder(nil, nil, username, layout.NewSpacer(), usernameInput)

		password := widget.NewLabel("password: ")
		passwordInput := widget.NewEntry()
		passwordRow := container.NewBorder(nil, nil, password, layout.NewSpacer(), passwordInput)

		appid := widget.NewLabel("appid:    ")
		appidText := widget.NewEntry()
		appidRow := container.NewBorder(nil, nil, appid, layout.NewSpacer(), appidText)

		appSecret := widget.NewLabel("appsecret:")
		appSecretText := widget.NewEntry()
		//多行输入
		appSecretText.MultiLine = true
		appSecretRow := container.NewBorder(nil, nil, appSecret, layout.NewSpacer(), appSecretText)

		url := widget.NewLabel("url:      ")
		urlText := widget.NewEntry()
		//多行输入
		urlText.MultiLine = true
		urlRow := container.NewBorder(nil, nil, url, layout.NewSpacer(), urlText)

		desc := widget.NewLabel("desc:     ")
		descText := widget.NewEntry()
		//多行输入
		descText.MultiLine = true
		descRow := container.NewBorder(nil, nil, desc, layout.NewSpacer(), descText)

		if types == "edit" {
			typeSelect.PlaceHolder = app.TypeText.Text
			typeText = app.TypeText.Text
			nameInput.SetText(app.NameText.Text)
			usernameInput.SetText(app.UsernameText.Text)
			passwordInput.SetText(app.PasswordText.Text)
			appidText.SetText(app.AppidText.Text)
			appSecretText.SetText(app.AppSecretText.Text)
			urlText.SetText(app.UrlText.Text)
			descText.SetText(app.DescText.Text)
		}

		participleHiddenBtn := widget.NewButton("显示或隐藏分词", func() {})
		saveBtn := widget.NewButton(" 保存 ", func() {

		})
		saveBtn.OnTapped = func() {
			if types == "add" {
				if typeText == "" {
					toast("请选择类型", w)
					return
				}
				if nameInput.Text == "" {
					toast("名称不能为空", w)
					return
				}
				sql.Insert("insert into account(type,name, username,password,appid,secret,url,desc) VALUES(?,?,?,?,?,?,?,?)", typeText, nameInput.Text, usernameInput.Text, passwordInput.Text, appidText.Text, appSecretText.Text, urlText.Text, descText.Text)

			} else if types == "edit" {
				sql.Update("update account set type=?,name=?,username=?,password=?,appid=?,secret=?,url=?,desc=? where id=?", typeText, nameInput.Text, usernameInput.Text, passwordInput.Text, appidText.Text, appSecretText.Text, urlText.Text, descText.Text, app.IdText.Text)

				app.IdText.SetText(app.IdText.Text)
				app.TypeText.SetText(typeText)
				app.NameText.SetText(nameInput.Text)
				app.UsernameText.SetText(usernameInput.Text)
				app.PasswordText.SetText(passwordInput.Text)
				app.AppidText.SetText(appidText.Text)
				app.AppSecretText.SetText(appSecretText.Text)
				app.DescText.SetText(descText.Text)

				index, err := strconv.Atoi(app.IndexText.Text)
				if err != nil {
					// 处理错误，例如打印错误信息
					app.List[0].Types = typeText
					app.List[0].Name = nameInput.Text
					app.List[0].Username = usernameInput.Text
					app.List[0].Password = passwordInput.Text
					app.List[0].Appid = appidText.Text
					app.List[0].Secret = appSecretText.Text
					app.List[0].Desc = descText.Text
				} else {
					app.List[index].Types = typeText
					app.List[index].Name = nameInput.Text
					app.List[index].Username = usernameInput.Text
					app.List[index].Password = passwordInput.Text
					app.List[index].Appid = appidText.Text
					app.List[index].Secret = appSecretText.Text
					app.List[index].Desc = descText.Text
				}

			}
			app.List = sql.Query("select * from account order by id desc")
			app.refreshList()
			w.Close()
		}
		cancelBtn := widget.NewButton(" 取消 ", func() {
			w.Close()
		})

		saveBtnLayout := container.New(layout.NewHBoxLayout(), participleHiddenBtn, saveBtn, cancelBtn)
		// btnLayout := container.NewCenter(saveBtnLayout)
		btnLayout := container.NewBorder(nil, nil, nil, saveBtnLayout, nil)
		addDataLayout := container.New(layout.NewVBoxLayout(), typeRow, typeStatusRow, nameRow, usernameRow, passwordRow, appidRow, appSecretRow, urlRow, descRow, btnLayout)

		wordsInput := widget.NewEntry()
		wordsInput.Wrapping = 2 // 开启自动换行
		//多行输入
		wordsInput.MultiLine = true
		/* wordsInput.OnChanged = func(text string) {
			app.startParticiple(text, w)
		} */
		participleBtn := widget.NewButton("开始分词", func() {
			app.startParticiple(wordsInput.Text, w)
		})

		wordsShow := widget.NewEntry()
		wordsShow.Wrapping = 2 // 开启自动换行
		//多行输入
		wordsShow.MultiLine = true
		// 禁用输入框
		wordsShow.Disable()
		clearWordsBtn := widget.NewButton("清空", func() {
			wordsShow.SetText("")
		})
		wordsTypeText := "name"
		wordsType := widget.NewSelect([]string{"name", "username", "password", "appid", "appsecret"}, func(s string) {
			wordsTypeText = s
		})

		wordsType.PlaceHolder = "name"
		addWordsBtn := widget.NewButton("添加关键词", func() {
			if wordsTypeText == "name" {
				nameInput.SetText(wordsShow.Text)
			} else if wordsTypeText == "username" {
				usernameInput.SetText(wordsShow.Text)
			} else if wordsTypeText == "password" {
				passwordInput.SetText(wordsShow.Text)
			} else if wordsTypeText == "appid" {
				appidText.SetText(wordsShow.Text)
			} else if wordsTypeText == "appsecret" {
				appSecretText.SetText(wordsShow.Text)
			}
			wordsShow.SetText("")
		})
		wordsLayout := container.New(layout.NewHBoxLayout(), clearWordsBtn, wordsType, addWordsBtn)
		app.ParticipleLayout = container.New(layout.NewVBoxLayout(), wordsInput, participleBtn, wordsShow, wordsLayout)

		app.WordListLayout = widget.NewList(
			func() int {
				return len(app.WordsList)
			},
			func() fyne.CanvasObject {
				return container.NewBorder(nil, nil, nil, nil, widget.NewLabel("item x.y"))
			},
			func(index widget.ListItemID, o fyne.CanvasObject) {

				title := app.WordsList[index]
				fmt.Println(title)

				o.(*fyne.Container).Objects[0].(*widget.Label).SetText(title)

			},
		)
		app.WordListLayout.OnSelected = func(index widget.ListItemID) {
			word := app.WordsList[index]

			wordsShow.SetText(wordsShow.Text + word)
		}

		// app.ParticipleLayout.Add(ListLayout)
		wordSideLayout := container.NewBorder(app.ParticipleLayout, nil, nil, nil, app.WordListLayout)
		wordSideLayout.Hidden = true
		/* scroll := container.NewHScroll(app.ParticipleLayout)
		scroll.SetMinSize(fyne.NewSize(100, 100)) // 设置滚动条的最小宽度为200像素 */
		// layout := container.NewBorder(nil, nil, addDataLayout, layout.NewSpacer(), app.ParticipleLayout)

		app.GridRows = 1
		// 使用两列的GridLayout
		layout := container.New(layout.NewGridLayoutWithRows(app.GridRows), addDataLayout, wordSideLayout)
		// layout := container.New(layout.NewHBoxLayout(), addDataLayout, app.ParticipleLayout)

		participleHiddenBtn.OnTapped = func() {
			app.GridRows = 2
			app.ParticipleLayout.Hidden = false
			wordSideLayout.Hidden = false

		}
		/* edit := widget.NewMultiLineEntry()
		addBtn := widget.NewButton(" 添加 ", func() {})

		editLayout := container.New(layout.NewVBoxLayout(), edit, addBtn)
		editLayout.Hide() */

		w.SetContent(layout)
		// w.SetContent(container.NewBorder(layout.NewSpacer(), layout.NewSpacer(), addDataLayout, layout.NewSpacer(), editLayout))
		// start 监听主窗口的关闭事件
		// 前提是必须设置主窗口
		w.SetOnClosed(func() {
			app.ChildWindow = nil
			w.Close()
		})
		// edn 监听主窗口的关闭事件
		// 设置窗口大小,
		w.Resize(fyne.NewSize(600, 320))
		w.CenterOnScreen()
		w.Show()
	}
}

// 导出
func (app *config) exportFile(win fyne.Window) func() {
	return func() {
		// defaultFilename := "account.txt" // 设置文件名称
		dialog.ShowFileSave(func(writer fyne.URIWriteCloser, err error) {
			if err != nil {
				dialog.ShowError(err, win)
				return
			}
			if writer == nil {
				log.Println("Cancelled")
				return
			}

			var str string
			for _, value := range app.List {

				curStr := "name: %s \ntype: %s \nusername: %s \npassword: %s \nappid: %s \nappSecret: %s \ndesc: %s \n\n"
				//字符串拼接
				str += fmt.Sprintf(curStr, value.Name, value.Types, value.Username, value.Password, value.Appid, value.Secret, value.Desc)
			}

			data := []byte(str)
			defer writer.Close()
			_, err = writer.Write(data)
			if err != nil {
				dialog.ShowError(err, win)
			}

		}, win)
	}
}

func (app *config) makeButtonList() fyne.CanvasObject {

	app.List = sql.Query("select * from account order by id desc")

	searchInput := widget.NewEntry()
	searchInput.SetPlaceHolder("请输入搜索关键词")
	// addBtn := widget.NewButton("添加", app.openAddWindow(win, "add"))
	homeBtn := widget.NewButton("首页", func() {
		app.welcomeLayout.Hidden = false
		app.infoLayout.Hidden = true
		// 清除选中状态
		app.ListLayout.UnselectAll()
	})
	refreshList := widget.NewButton("刷新", func() {})
	empty := widget.NewButton("清空", func() {
		searchInput.SetText("")
	})

	app.ListLayout = widget.NewList(
		func() int {
			return len(app.List)
		},
		func() fyne.CanvasObject {
			return container.NewBorder(nil, nil, widget.NewLabel("类型"), nil, widget.NewLabel("item x.y"))

			// return container.New(layout.NewHBoxLayout(), widget.NewButton("type", nil), widget.NewLabel("item x.y"), widget.NewButton("复制", nil))
		},
		func(index widget.ListItemID, o fyne.CanvasObject) {

			title := app.List[index].Name
			/* if utf8.RuneCountInString(title) > 10 {
				//截取字符串
				title = base.Substr(app.List[index].Name, 0, 10) + "..."
			} */
			o.(*fyne.Container).Objects[1].(*widget.Label).SetText(app.List[index].Types)
			o.(*fyne.Container).Objects[0].(*widget.Label).SetText(title)

			/* copyBtn := o.(*fyne.Container).Objects[2].(*widget.Button)
			copyBtn.OnTapped = func() {
				var str string = "/======= start %s =======/\nname: %s\nusername: %s\npassword: %s\nappid: %s\nappSecret: %s\ndesc: %s\n/======= end =======/"
				//字符串拼接
				copyContent := fmt.Sprintf(str, app.List[index].Name, app.List[index].Username, app.List[index].Username, app.List[index].Password, app.List[index].Appid, app.List[index].Secret, app.List[index].Desc)
				base.CopyText(copyContent)
			} */

		},
	)

	app.ListLayout.OnSelected = func(index widget.ListItemID) {
		app.IndexText.SetText(strconv.Itoa(index))
		app.IdText.SetText(strconv.Itoa(app.List[index].Id))
		app.TypeText.SetText(app.List[index].Types)
		app.NameText.SetText(app.List[index].Name)
		app.UsernameText.SetText(app.List[index].Username)
		app.PasswordText.SetText(app.List[index].Password)
		app.AppidText.SetText(app.List[index].Appid)
		app.AppSecretText.SetText(app.List[index].Secret)
		app.UrlText.SetText(app.List[index].Url)
		app.DescText.SetText(app.List[index].Desc)
		app.welcomeLayout.Hidden = true
		app.infoLayout.Hidden = false
		// 刷新列表组件
		app.ListLayout.Refresh()
	}
	searchInput.OnChanged = func(content string) {
		app.List = sql.Query("select * from account where name like '%" + content + "%' order by id desc")
		// 刷新列表组件
		app.ListLayout.Refresh()
	}
	headerFunc := container.New(layout.NewHBoxLayout(), homeBtn, refreshList, empty)
	headerLayout := container.New(layout.NewVBoxLayout(), searchInput, headerFunc)
	sideLayout := container.NewBorder(headerLayout, nil, nil, nil, app.ListLayout)
	return sideLayout
}

func (app *config) makeUI(win fyne.Window, ListLayout fyne.CanvasObject) fyne.CanvasObject {
	welcome := widget.NewLabel("欢迎您使用-账号管理器")

	addBtn := widget.NewButton(" 添加 ", app.openAddWindow(win, "add"))
	exportBtn := widget.NewButton(" 导出 ", app.exportFile(win))
	lockBtn := widget.NewButton(" 锁屏 ", app.lockAccount(win))

	welcomeLayoutRow_1 := container.New(layout.NewHBoxLayout(), widget.NewLabel("   "), addBtn, exportBtn, lockBtn)
	app.welcomeLayout = container.New(layout.NewVBoxLayout(), container.NewCenter(welcome), welcomeLayoutRow_1)

	app.IndexText = widget.NewLabel("")
	IndexTextLayout := container.New(layout.NewHBoxLayout(), app.IndexText)
	IndexTextLayout.Hide()
	app.IdText = widget.NewLabel("")
	idTextLayout := container.New(layout.NewHBoxLayout(), app.IdText)
	idTextLayout.Hide()

	var selectAllCheckStatus bool = false
	selectAllCheck := widget.NewCheck("", func(on bool) {
		selectAllCheckStatus = on
		app.UsernameCheckStatus = on
	})
	selectAllText := widget.NewLabel("全选")
	editBtn := widget.NewButton(" 编辑 ", func() {})
	editBtn.OnTapped = app.openAddWindow(win, "edit")

	refreshBtn := widget.NewButton(" 刷新 ", func() {})
	refreshBtn.OnTapped = app.refreshList()
	//抖音服务商版本管理工具
	douYinServiceBtn := widget.NewButton("打开抖音服务商", app.openVersionManageTools(win))
	//删除
	delBtn := widget.NewButton(" 删除 ", app.delCurrentInfo(win, app.ListLayout))
	delBtn.Importance = widget.DangerImportance
	editLeftLayout := container.New(layout.NewHBoxLayout(), selectAllCheck, selectAllText, editBtn, refreshBtn, douYinServiceBtn)
	selectAllTextLayout := container.NewBorder(widget.NewLabel(""), nil, nil, delBtn, editLeftLayout)
	// selectAllTextLayout := container.New(layout.NewHBoxLayout(), editLayout)

	var typeNameCheckStatus bool = false
	typeNameCheck := widget.NewCheck("", func(on bool) { typeNameCheckStatus = on })
	typeName := widget.NewLabel("type:     ")
	app.TypeText = widget.NewLabel("")
	typeTextLayout := container.New(layout.NewHBoxLayout(), typeNameCheck, typeName, app.TypeText)
	typeTextRow := container.NewBorder(nil, nil, typeTextLayout, nil, nil)

	// var nameCheckStatus bool = false
	nameCheck := widget.NewCheck("", func(on bool) {
		// nameCheckStatus = on
	})
	name := widget.NewLabel("name:     ")
	app.NameText = widget.NewLabel("")

	nameCopyBtn := widget.NewButton("copy", func() {
		base.CopyText(app.NameText.Text)
	})

	nameTextLayout := container.New(layout.NewHBoxLayout(), nameCheck, name, app.NameText)
	nameRow := container.NewBorder(nil, nil, nil, nameCopyBtn, nameTextLayout)

	usernameCheck := widget.NewCheck("", func(on bool) { app.UsernameCheckStatus = on })
	username := widget.NewLabel("username:")
	// app.UsernameText = widget.NewLabel("")
	app.UsernameText = widget.NewButton("copy", func() {
		base.CopyText(app.UsernameText.Text)
	})
	usernameCopyBtn := widget.NewButton("copy", func() {
		base.CopyText(app.UsernameText.Text)
	})
	usernameTextLayout := container.New(layout.NewHBoxLayout(), usernameCheck, username, app.UsernameText)
	usernameRow := container.NewBorder(nil, nil, nil, usernameCopyBtn, usernameTextLayout)

	var passwordCheckStatus bool = false
	passwordCheck := widget.NewCheck("", func(on bool) { passwordCheckStatus = on })
	password := widget.NewLabel("password: ")
	app.PasswordText = widget.NewLabel("")
	passwordTextCopyBtn := widget.NewButton("copy", func() {
		base.CopyText(app.PasswordText.Text)
	})
	passwordTextLayout := container.New(layout.NewHBoxLayout(), passwordCheck, password, app.PasswordText)
	passwordRow := container.NewBorder(nil, nil, nil, passwordTextCopyBtn, passwordTextLayout)

	var appidCheckStatus bool = false
	appidCheck := widget.NewCheck("", func(on bool) { appidCheckStatus = on })
	appid := widget.NewLabel("appid:    ")
	app.AppidText = widget.NewLabel("")
	appidTextCopyBtn := widget.NewButton("copy", func() {
		base.CopyText(app.AppidText.Text)
	})
	appidTextLayout := container.New(layout.NewHBoxLayout(), appidCheck, appid, app.AppidText)
	appidRow := container.NewBorder(nil, nil, nil, appidTextCopyBtn, appidTextLayout)

	var appSecretCheckStatus bool = false
	appSecretCheck := widget.NewCheck("", func(on bool) { appSecretCheckStatus = on })
	appSecret := widget.NewLabel("appsecret:")
	app.AppSecretText = widget.NewLabel("")
	appSecretTextCopyBtn := widget.NewButton("copy", func() {
		base.CopyText(app.AppSecretText.Text)
	})
	appSecretTextLayout := container.New(layout.NewHBoxLayout(), appSecretCheck, appSecret, app.AppSecretText)
	appSecretRow := container.NewBorder(nil, nil, nil, appSecretTextCopyBtn, appSecretTextLayout)

	// var urlCheckStatus bool = false
	urlCheck := widget.NewCheck("", func(on bool) { fmt.Println("checked", on) })
	url := widget.NewLabel("url:      ")
	app.UrlText = widget.NewLabel("")
	openUrlBtn := widget.NewButton("打开", func() {
		if app.UrlText.Text == "" {
			return
		}
		cmd := exec.Command("cmd", "/c", "start", app.UrlText.Text)
		cmd.Start()

	})
	urlTextLayout := container.New(layout.NewHBoxLayout(), urlCheck, url, app.UrlText)
	urlRow := container.NewBorder(nil, nil, nil, openUrlBtn, urlTextLayout)

	var descCheckStatus bool = false
	descCheck := widget.NewCheck("", func(on bool) { descCheckStatus = on })
	desc := widget.NewLabel("desc:     ")
	app.DescText = widget.NewLabel("")
	descTextLayout := container.New(layout.NewHBoxLayout(), descCheck, desc, app.DescText)
	descRow := container.NewBorder(nil, nil, descTextLayout, nil, nil)

	copyBtn := widget.NewButton(" 自由组合复制 ", func() {
		var (
			typeText      string
			usernameText  string
			passwordText  string
			appidText     string
			appSecretText string
			descText      string
		)
		var str string = `/****** start %s *******/
	type:      %s
	username:  %s
	password:  %s
	appid:     %s
	appSecret: %s
	desc:      %s
/****** end *******/
		`
		if selectAllCheckStatus {
			typeNameCheckStatus = true
			app.UsernameCheckStatus = true
			passwordCheckStatus = true
			appidCheckStatus = true
			appSecretCheckStatus = true
			descCheckStatus = true

			typeText = app.TypeText.Text
			usernameText = app.UsernameText.Text
			passwordText = app.PasswordText.Text
			appidText = app.AppidText.Text
			appSecretText = app.AppSecretText.Text
			descText = app.DescText.Text
			//字符串拼接并复制
		} else {

			if typeNameCheckStatus {
				typeText = app.TypeText.Text
			}
			if app.UsernameCheckStatus {
				usernameText = app.UsernameText.Text
			}
			if passwordCheckStatus {
				passwordText = app.PasswordText.Text
			}
			if appidCheckStatus {
				appidText = app.AppidText.Text
			}
			if appSecretCheckStatus {
				appSecretText = app.AppSecretText.Text
			}
			if descCheckStatus {
				descText = app.DescText.Text
			}
		}
		//字符串拼接并复制
		copy := app.strConcatCopy(str, app.NameText.Text, typeText, usernameText, passwordText, appidText, appSecretText, descText)
		copy()
	})
	copyAllBtn := widget.NewButton("   复制所有   ", app.copyAllInfo())

	copyUserNameBtn := widget.NewButton("复制用户名密码", func() {
		var str string = `
		/****** start %s *******/
			username: %s
			password: %s
		/****** end *******/
		`
		//字符串拼接并复制
		copy := app.strConcatCopy(str, app.NameText.Text, app.UsernameText.Text, app.PasswordText.Text)
		copy()
	})
	copyAppidSecretBtn := widget.NewButton("复制appid,secret", func() {
		var str string = `
		/****** start %s *******/
			appid: %s
			appSecret: %s
		/****** end *******/
		`
		//字符串拼接并复制
		copy := app.strConcatCopy(str, app.NameText.Text, app.AppidText.Text, app.AppSecretText.Text)
		copy()
	})
	copyFileBtn := widget.NewButton("复制放到文件中", func() {
		var str string = "\n公司全称：%s\nusername: %s\npassword: %s\nappid: %s\n"
		//字符串拼接并复制
		copy := app.strConcatCopy(str, app.NameText.Text, app.UsernameText.Text, app.PasswordText.Text, app.AppidText.Text)
		copy()
	})
	copyVsCodeBtn := widget.NewButton("复制要验证码词", func() {
		str := "%s 这个客户需要一下验证码，还没有发"
		//字符串拼接并复制
		copy := app.strConcatCopy(str, app.UsernameText.Text)
		copy()
	})
	copyConfigBtn := widget.NewButton(" 复制配置信息 ", func() {
		str := "\n抖音服务商小程序配置一下\n\n公司名称: %s \n交付中心ID: \nappid: %s"
		//字符串拼接并复制
		copy := app.strConcatCopy(str, app.NameText.Text, app.AppidText.Text)
		copy()
	})
	copyDoyYinBtn := widget.NewButton(" 导入抖音服务商 ", func() {

		// 指定要打开的.exe文件的路径
		// exePath := "D:\\raymond\\tiktok-audit-tool\\tiktok-audit-tool.exe"
		// exePath := "D:/project/go/src/tiktok-audit-tool/tiktok-audit-tool.exe"

		// 创建命令对象
		cmd := exec.Command(tatPath[0]+tatPath[1], app.NameText.Text, app.AppidText.Text)
		// 设置工作目录
		cmd.Dir = tatPath[0]
		// 执行命令
		err := cmd.Run()
		if err != nil {
			fmt.Println("无法打开指定的.exe文件:", err)
			os.Exit(1)
		}
	})

	// 使用网格布局对按钮进行布局
	// gridLayout := container.New(layout.NewGridLayout(4), copyBtn, copyAllBtn, copyUserNameBtn, copyAppidSecretBtn, copyFileBtn, copyVsCodeBtn, copyDoyYinBtn)
	// gridLayout := container.New(layout.NewGridWrapLayout(fyne.NewSize(50, 50)), copyBtn, copyAllBtn, copyUserNameBtn, copyAppidSecretBtn, copyFileBtn, copyVsCodeBtn, copyDoyYinBtn)
	copyLayout := container.New(layout.NewHBoxLayout(), copyBtn, copyAllBtn, copyUserNameBtn, copyAppidSecretBtn)
	copyLayout2 := container.New(layout.NewHBoxLayout(), copyFileBtn, copyVsCodeBtn, copyConfigBtn, copyDoyYinBtn)
	app.infoLayout = container.New(layout.NewVBoxLayout(), idTextLayout, selectAllTextLayout, typeTextRow, nameRow, usernameRow, passwordRow, appidRow, appSecretRow, urlRow, descRow, copyLayout, copyLayout2)
	//隐藏，点击右侧更表时显示
	app.infoLayout.Hidden = true
	rightAllLayout := container.New(layout.NewVBoxLayout(), app.welcomeLayout, app.infoLayout)
	// app.VScroll = container.NewVScroll(container.NewVBox(app.List...))
	app.VScroll = container.NewVScroll(ListLayout)
	allLayout := container.NewHSplit(app.VScroll, container.NewScroll(rightAllLayout))

	// fmt.Printf("%T", infoLayout)
	// fmt.Printf("%T", vScroll)

	allLayout.Offset = 0.35

	return allLayout
}
func (app *config) updateInfo(params map[string]string) {
	app.IdText.SetText(params["IdText"])
	app.NameText.SetText(params["nameInput"])
	app.UsernameText.SetText(params["usernameInput"])
	app.PasswordText.SetText(params["passwordInput"])
	app.AppidText.SetText(params["appidText"])
	app.AppSecretText.SetText(params["appSecretText"])
	app.DescText.SetText(params["descText"])
}

// 打开抖音服务商版本管理工具
func (app *config) openVersionManageTools(win fyne.Window) func() {
	return func() {
		// 指定要打开的.exe文件的路径
		// exePath := "D:/raymond/tiktok-audit-tool/tiktok-audit-tool.exe"
		// exePath := "D:/project/go/src/tiktok-audit-tool/tiktok-audit-tool.exe"

		// 创建命令对象
		cmd := exec.Command(tatPath[0] + tatPath[1])
		// 设置工作目录
		// cmd.Dir = "D:/project/go/src/tiktok-audit-tool/"
		cmd.Dir = tatPath[0]
		// 执行命令
		err := cmd.Run()
		if err != nil {
			fmt.Println("无法打开指定的.exe文件:", err)
			os.Exit(1)
		}
	}
}

// 复制当前所有信息
func (app *config) copyAllInfo() func() {
	return func() {
		var str string = `
		/****** start %s *******/
			username: %s
			password: %s
			appid: %s
			appSecret: %s
			desc: %s
		/****** end *******/
		`
		//字符串拼接并复制
		app.strConcatCopy(str, app.NameText.Text, app.UsernameText.Text, app.PasswordText.Text, app.AppidText.Text, app.AppSecretText.Text, app.DescText.Text)
	}
}

// 删除列表中的当前信息
func (app *config) delCurrentInfo(win fyne.Window, ListLayout fyne.CanvasObject) func() {
	return func() {

		cnf := dialog.NewConfirm("提示", "您是否要删除当前信息", func(response bool) {
			if response {
				sql.Delete("delete from account where id=?", app.IdText.Text)

				app.List = sql.Query("select * from account order by id desc")
				ListLayout.Refresh() // 刷新列表以显示更改

				app.IndexText.SetText("")
				app.IdText.SetText("")
				app.TypeText.SetText("")
				app.NameText.SetText("")
				app.UsernameText.SetText("")
				app.PasswordText.SetText("")
				app.AppidText.SetText("")
				app.AppSecretText.SetText("")
				app.DescText.SetText("")
				app.welcomeLayout.Hidden = false
				app.infoLayout.Hidden = true
				// 清除选中状态
				app.ListLayout.UnselectAll()
			}
		}, win)
		cnf.SetDismissText("取消")
		cnf.SetConfirmText("确认")
		cnf.Show()

	}
}

func (app *config) refreshList() func() {
	return func() {

		app.List = sql.Query("select * from account order by id desc")
		// 刷新列表组件
		app.ListLayout.Refresh()

	}
}

// 锁屏
func (app *config) lockAccount(win fyne.Window) func() {
	return func() {
		cfg.login(win)
		win.Hide()
	}
}

// 字符串拼接并复制
func (app *config) strConcatCopy(str string, params ...interface{}) func() {
	return func() {
		//字符串拼接
		copyContent := fmt.Sprintf(str, params...)
		base.CopyText(copyContent)
	}
}

// 开始分词
func (app *config) startParticiple(text string, win fyne.Window) {
	/* text := `@蛟 菏泽祥盛人力资源有限公司      tt13d6656b8ce732f101  账号：13258025511
	密码：zhang001
	id:621
	管理账号
	账号：xiangshengrenli 密码：xiangsheng2024
	转化账号：
	账号：xs_shiyanliang 密码：xiangsheng2024
	已经备案 订单已上传 需要发版` */

	/* text := `公司全称：重庆优快人力资源管理公司
	公司地址：重庆市万州区高峰街道楼子中路1号4号楼
	账号：19132895814
	密码：huijie001
	负责人及电话：向帆 15123562109  19132895814
	转化人员姓名电话(可以多个)：王秋菊 15123519383；13330307765
	开发人（谁签的）：代运营客户` */

	// 初始化分词器
	x := gojieba.NewJieba()
	defer x.Free()

	x.AddWord("人力资源管理公司")
	x.AddWord("人力资源管理有限公司")
	x.AddWord("有限责任公司")
	x.AddWord("人力资源服务有限公司")
	x.AddWord("人力资源集团有限公司")
	x.AddWord("外包有限公司")
	x.AddWord("管理有限公司")
	x.AddWord("有限公司")
	// 使用HMM模型进行分词
	words := x.Cut(text, true)
	for _, word := range words {
		if word != " " {
			app.WordsList = append(app.WordsList, word)
		}
	}
	app.WordListLayout.Refresh()
	/* app.ParticipleLayout.Refresh()
	// 刷新容器
	win.Canvas().Refresh(nil) */
	/* fmt.Println("\n\n")
	// 使用搜索引擎模式进行分词
	searchWords := x.CutForSearch(text, true)
	for _, word := range searchWords {
		fmt.Println(word)
	} */

}

// 提示弹窗
func toast(title string, win fyne.Window) {
	dialog.ShowInformation("提示", title, win)
}
